using AOT;
using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;

namespace DA.AnimGraph {
    [BurstCompile]
    public unsafe partial struct CurveSamplerPerk {
        public struct SetupData {
            public AUID curveID;
        }

        AUID curveID;

        [BurstCompile]
        static void SetupLink(CurveSamplerPerk* self, PerkLink* link) {
            link->SetupPerk(self);
        }

        void Setup(in SetupData setupData) {
            curveID = setupData.curveID;
        }

        [BurstCompile]
        [MonoPInvokeCallback(typeof(PerkLink.EvaluateDelegate))]
        private static void Evaluate(in void* ptr, in TimeState* timeState, bool onEnter, in EvaluateContext evaluateContext, in GraphContext graphContext, ref UpstreamData upstream, ref ScopeContext scope) {
            scope.SetHasCurve();
        }

        [BurstCompile]
        [MonoPInvokeCallback(typeof(PerkLink.PostEvaluateDelegate))]
        private static void PostEvaluate(in void* ptr, in TimeState* timeState, in EvaluateContext evaluateContext, in GraphContext graphContext, ref UpstreamData upstream, ref PostScopeContext postScope) {
            if (timeState != null && graphContext.curveBoard.IsValid) {
                var self = (CurveSamplerPerk*)ptr;

                Curve curve = default;
                if (graphContext.curveBoard.GetCurve(self->curveID, ref curve)) {
                    var sample = curve.Sample(timeState->time);
                    postScope.AddCurve(sample);
                }
            }
        }

    }
}
